All files / src/hooks useUnreadTickets.ts

0% Statements 0/14
0% Branches 0/6
0% Functions 0/2
0% Lines 0/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                                                                       
import { useQuery } from '@tanstack/react-query';
import { apiService } from '@/services/api';
import { useAuth } from '@/contexts/AuthContext';
import i18n from '@/lib/i18n';
 
interface UnreadTicketsResponse {
  total_unread: number;
}
 
export function useUnreadTickets() {
  const { user } = useAuth();
 
  return useQuery({
    queryKey: ['unread-tickets', user?.role],
    queryFn: async () => {
      if (!user) {
        throw new Error(i18n.t('auth.session.sessionExpired'));
      }
 
      // Use the correct endpoint based on user role
      const endpoint = user.role === 'admin'
        ? '/api/admin/tickets/unread-count'
        : '/api/reseller/tickets/unread-count';
 
      const result = await apiService.get<UnreadTicketsResponse>(endpoint);
      if (result.success) {
        return result.data;
      }
      throw new Error(result.error.details);
    },
    enabled: !!user, // Only run query if user is authenticated
    refetchInterval: 30000, // Check every 30 seconds
    staleTime: 10000, // Consider data stale after 10 seconds
  });
}